package com.todod.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.entity.QueryEntry;
import com.todod.mapper.OperateLogMapper;
import com.todod.mapper.UserMapper;
import com.todod.model.OperateLog;
import com.todod.model.User;

@Service
@Transactional
public class OperateLogService {
	Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private OperateLogMapper operateLogMapper;

	@Autowired
	private UserMapper userMapper;

	/**
	 * 分页查询
	 * 
	 * @Title: getPage
	 * @Description:
	 * @author gsh
	 * @date 2019年10月11日
	 * @param queryEntry
	 * @return
	 */
	public IPage<OperateLog> getPage(QueryEntry queryEntry) {
		Page<OperateLog> page = new Page<OperateLog>(queryEntry.getPage(), queryEntry.getLimit());

		QueryWrapper<OperateLog> queryWrapper = new QueryWrapper<OperateLog>();

		if (!StringUtils.isBlank(queryEntry.getName())) {
			queryWrapper.like("ol_user_name", "%" + queryEntry.getName() + "%");
		}
		if (!StringUtils.isBlank(queryEntry.getQueryDate())) {
			queryWrapper.ge("ol_create_time", "" + queryEntry.getQueryDate() + " 00:00:00" + "");
			queryWrapper.le("ol_create_time", "" + queryEntry.getQueryDate() + " 24:00:00" + "");
		}
		queryWrapper.orderByDesc("ol_create_time");
		IPage<OperateLog> ipage = operateLogMapper.selectPage(page, queryWrapper);

		return ipage;
	}

	public void saveOperateLog(Long _userId, String content, String ipAddr, String methodName, String clazzName) {

		User user = userMapper.selectById(_userId);

		OperateLog operateLog = new OperateLog();
		operateLog.setId(SnowflakeIdGenerator.getId());
		operateLog.setCreateUserId(_userId);
		operateLog.setCreateTime(new Date());
		operateLog.setDepartmentId(user.getDepartid());

		operateLog.setClassName(clazzName);
		operateLog.setContent(content);
		operateLog.setIpAddr(ipAddr);
		operateLog.setMethodName(methodName);
		operateLog.setUserName(user.getNickName());
		operateLog.setUserId(_userId);

		operateLogMapper.insert(operateLog);
	}

	public void delete(User user, String date, List<String> dIds) {

		List<OperateLog> list = getBycreateTime(date, dIds);

		for (OperateLog operateLog : list) {
			operateLogMapper.deleteById(Long.valueOf(operateLog.getId()));
		}
	}

	/**
	 * 
	 * @Title: getBycreateTime
	 * @Description: 根据日期获取
	 * @author：gsh
	 * @date: 2018-9-5
	 * @param createTime
	 * @param dIds
	 * @param companyId
	 * @return
	 * @return List<OperateLog>
	 */
	public List<OperateLog> getBycreateTime(String createTime, List<String> dIds) {
		QueryWrapper<OperateLog> queryWrapper = new QueryWrapper<OperateLog>();
		queryWrapper.ge("ol_create_time", "" + createTime + " 00:00:00" + "");
		queryWrapper.le("ol_create_time", "" + createTime + " 24:00:00" + "");
		queryWrapper.in("ol_department_id", dIds);
		queryWrapper.orderByDesc("ol_create_time");

		return operateLogMapper.selectList(queryWrapper);
	}

}
